//
//  DetailsViewController.m
//  ECO
//
//  Created by zl on 2019/10/12.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "DetailsViewController.h"
#import "DetailsTableViewCell.h"// 资料
#import "BrowseTableViewCell.h" // 订单浏览
#import "WithdrawTableViewCell.h"// 提现列表
#import "IndentViewController.h"// 订单控制器
#import "DetailModel.h"// 资料详情模型
#import "WithdrawModel.h" // 提现列表模型
@interface DetailsViewController ()<UITableViewDelegate,UITableViewDataSource,BrowseTableViewCellDelegate>{
    NSDictionary *DataDiction;
    NSDictionary *Diction;
    NSDictionary *YjMoney;// 总佣金
    NSDictionary *Alldic;// 总佣金
}
@property(nonatomic , strong)UITableView *ojtable;
@property(nonatomic , strong)NSMutableArray *ListArray;
@end

@implementation DetailsViewController
- (UITableView *)ojtable{
    if (!_ojtable) {
        _ojtable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight -100)];
        _ojtable.delegate = self;
        _ojtable.dataSource = self;
        _ojtable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _ojtable;
}
- (NSMutableArray *)ListArray{
    if (!_ListArray) {
        _ListArray = [[NSMutableArray alloc]init];
    }
    return _ListArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
// 取消组头悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 70;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.ojtable];
    [self FsNumLoadData]; // 获取用户信息及粉丝数
    [self moneyLoadData]; // 查询 总提现金额 佣金
    [self indentLoadData]; // 获取 全部订单数，有效订单数 退款订单 商品总佣金 用户总佣金
    [self WithdrawDepositLoadData];// 提现管理列表
}
// 获取用户信息及粉丝数
- (void)FsNumLoadData{
    DataDiction = [NSDictionary dictionary];
    Diction = [NSDictionary dictionary];
    NSDictionary *dic = @{
                        @"userId":self.YhUserId
                        };
    [HttpsTool get:@"/kf/eco/user/findUser" params:dic success:^(id response) {
        NSLog(@"获取用户信息及粉丝数 : %@",response);
        DetailModel *model = [DetailModel yy_modelWithJSON:response];
        DataDiction = [model.data yy_modelToJSONObject];
        Diction = [model.data.user yy_modelToJSONObject];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.ojtable reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
//查询 总提现金额 佣金
- (void)moneyLoadData{
    YjMoney = [NSDictionary dictionary];
    NSDictionary *dict = @{
                        @"appUserId":self.YhUserId,
                        @"PC":@"PC"
                        };
    [HttpsTool get:@"/1.0/withdrawal/total/money" params:dict success:^(id response) {
        NSLog(@"查询 总提现金额 : %@",response);
        YjMoney = [response yy_modelToJSONObject];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.ojtable reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"查询 总提现金额 : %@",error);
    }];
}
// 获取 全部订单数，有效订单数 退款订单 商品总佣金 用户总佣金
- (void)indentLoadData{
    Alldic = [NSDictionary dictionary];
    NSDictionary *dict = @{
                        @"appUserId":self.YhUserId,
                        @"PC":@"PC"
                        };
    [HttpsTool get:@"/1.0/order/total" params:dict success:^(id response) {
        NSLog(@"获取 全部订单数，有效订单数 退款订单 商品总佣金 用户总佣金 : %@",response);
        Alldic = [response[@"data"] yy_modelToJSONObject];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.ojtable reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"获取 全部订单数，有效订单数 退款订单 商品总佣金 用户总佣金 : %@",error);
    }];
}
// 提现管理列表
- (void)WithdrawDepositLoadData{
    NSDictionary *dict = @{
                        @"appUserId":self.YhUserId,
                        @"PC":@"PC"
                        };
    [HttpsTool get:@"/web/withdrawal/selectWithdrawalList" params:dict success:^(id response) {
        NSLog(@"提现管理列表 : %@",response);
        WithdrawModel *model = [WithdrawModel yy_modelWithJSON:response];
        self.ListArray = [model.data.list yy_modelToJSONObject];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.ojtable reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"提现管理列表 : %@",error);
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
        if (self.ListArray.count > 0) {
            return self.ListArray.count;
        }else{
            return 0;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Dcell"];
        if (!cell) {
            cell = [[DetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Dcell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",Diction[@"userImgUrl"]]] placeholderImage:[UIImage imageNamed:@"头像"]];
        cell.Name.text = Diction[@"userName"];
        cell.yhID.text = [NSString stringWithFormat:@"用户id：%@",Diction[@"userId"]];
        cell.FsNum.text = DataDiction[@"fensiSize"];
        cell.YeNum.text = [NSString stringWithFormat:@"￥%@",Diction[@"balance"]];
        if (![NSString isNULLString:YjMoney[@"data"]]) {
            cell.YjNum.text = [NSString stringWithFormat:@"￥%@",YjMoney[@"data"]];
        }else{
            cell.YjNum.text = @"￥0.00";
        }
        
        cell.FsTitle.text = @"粉丝数";
        cell.YeTitle.text = @"可提现余额";
        cell.YjTitle.text = @"总佣金";
        return cell;
    }else if (indexPath.section == 1){
        BrowseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BrowseCell"];
        if (!cell) {
            cell = [[BrowseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BrowseCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        [cell initwithDictionary:Alldic];
        return cell;
    }else{
        WithdrawTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawCell"];
        if (!cell) {
            cell = [[WithdrawTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WithdrawCell"];
        }
        NSDictionary *dic = self.ListArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *str2 = [dic[@"createTime"] substringWithRange:NSMakeRange(5,5)];
        NSString *SQstrUrl = [str2 stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
        cell.TimeTitle.text = SQstrUrl;
        // 年份
        NSString *KSTimeStr = [dic[@"createTime"] substringToIndex:4];
        cell.YearTitle.text = [NSString stringWithFormat:@"%@年",KSTimeStr];
        cell.Title.text = [NSString stringWithFormat:@"提现%@元到支付宝",dic[@"amountMoney"]];
//        处理状态 提现状态 0 未处理 2 web已处理 3 已拒绝(未扣除) 4已拒绝(已扣除) 6(app提现成功，转账也成功)7(app提现失败)，默认 0
//        订单状态,12即将入账，3结算、14已入账，13失效
        if ([dic[@"status"] isEqualToString:@"0"]) {
            cell.Status.text = @"未处理";
            cell.Status.textColor = [UIColor colorWithHexString:@"#FFA432"];
        }else if ([dic[@"status"] isEqualToString:@"6"]){
            cell.Status.text = @"已处理";
            cell.Status.textColor = [UIColor colorWithHexString:@"#40D0C0"];
        }else if ([dic[@"status"] isEqualToString:@"7"]){
            cell.Status.text = @"处理失败";
            cell.Status.textColor = [UIColor colorWithHexString:@"#FF0000"];
        }else{
            cell.Status.text = @"默认";
            cell.Status.textColor = [UIColor colorWithHexString:@"#333333"];
        }
        NSString *TimeStr = [dic[@"createTime"] substringWithRange:NSMakeRange(11,5)];
        cell.Time.text = TimeStr;
        if (indexPath.row == self.ListArray.count-1) {
            cell.lineView.hidden = YES;
        }
        return cell;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *View = [[UIView alloc]init];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 200, 25)];
    NSArray *arr = @[@"资料详情",@"订单概览",@"提现列表"];
    label.text = arr[section];
    label.font = [UIFont systemFontOfSize:18];
    [View addSubview:label];
    return View;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 188;
    }else if (indexPath.section == 1){
        return 200;
    }else{
        return 140;
    }
}
// 订单代理跳转
- (void)BrowseTableViewCellClick:(NSString *)index{
    NSLog(@"%@",index);
    IndentViewController *Indent = [[IndentViewController alloc]init];
    Indent.Index = index;
    Indent.appUserId = self.YhUserId;
    [self.navigationController pushViewController:Indent animated:YES];
}

@end
